import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import TaskList from '@/components/task/list'
import TaskAdd from '@/components/task/add'
import TaskDetail from '@/components/task/detail'

import WareList from '@/components/ware/list'
import WareAdd from '@/components/ware/add'
import WareDetail from '@/components/ware/detail'

import WareFleaList from '@/components/wareflea/list'
import WareFleaDetail from '@/components/wareflea/detail'

import OrderWareList from '@/components/order/ware/list'
import OrderWareDetail from '@/components/order/ware/detail'
import OrderWareFleaList from '@/components/order/wareflea/list'
import OrderWareFleaDetail from '@/components/order/wareflea/detail'
import OrderLoterryList from '@/components/order/loterry/list'
import OrderLoterryDetail from '@/components/order/loterry/detail'
import OrderRedPackList from '@/components/order/redpack/list'
import OrderRedPackDetail from '@/components/order/redpack/detail'

import VenderList from '@/components/vender/list'
import VenderAdd from '@/components/vender/add'
import VenderDetail from '@/components/vender/detail'

import CategoryManage from '@/components/category/manage'
import CategoryPhoto from '@/components/category/photo'
import CategoryPhotoDetail from '@/components/category/photoDetail'

import Login from '@/components/login/login'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login/login'
    },
    {
      path:'/login/login',
      name:'loginLogin',
      component:Login
    },
    {
      path:'/task/list',
      name:'taskList',
      component:TaskList,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/task/add',
      name:'taskAdd',
      component:TaskAdd,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/task/:id',
      name:'taskDetail',
      component:TaskDetail,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/ware/list',
      name:'wareList',
      component:WareList,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/ware/add',
      name:'wareAdd',
      component:WareAdd,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/ware/:id',
      name:'wareDetail',
      component:WareDetail,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/wareflea/list',
      name:'wareFleaList',
      component:WareFleaList,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/wareflea/:id',
      name:'wareFleaDetail',
      component:WareFleaDetail,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/order/ware',
      name:'orderWareList',
      component:OrderWareList,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/order/ware/:id',
      name:'orderWareDetail',
      component:OrderWareDetail,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/order/wareflea',
      name:'orderWareFleaList',
      component:OrderWareFleaList,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/order/wareflea/:id',
      name:'orderWareFleaDetail',
      component:OrderWareFleaDetail,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/order/loterry',
      name:'orderLoterryList',
      component:OrderLoterryList,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/order/loterry/:id',
      name:'orderLoterryDetail',
      component:OrderLoterryDetail,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/order/redpack',
      name:'orderRedPackList',
      component:OrderRedPackList,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/order/redpack/:id',
      name:'orderRedPackDetail',
      component:OrderRedPackDetail,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/category/list',
      name:'CategoryManage',
      component:CategoryManage,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/photo/list',
      name:'CategoryPhoto',
      component:CategoryPhoto,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/photo/:id',
      name:'CategoryPhotoDetail',
      component:CategoryPhotoDetail,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/vender/list',
      name:'VenderList',
      component:VenderList,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/vender/add',
      name:'VenderAdd',
      component:VenderAdd,
      meta:{
        requireAuth: true
      }
    },
    {
      path:'/vender/:id',
      name:'VenderDetail',
      component:VenderDetail,
      meta:{
        requireAuth: true
      }
    }

  ]
})

// 注册全局钩子用来拦截导航
router.beforeEach((to, from, next) => {
  const token = store.state.token
  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
    if (token) { // 通过vuex state获取当前的token是否存在
      next()
    } else {
      next({
        path: '../login/login'
        // query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  } else {
    next()
  }
})

export default router
